package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BusinessException;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl
        extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        //1、保存套餐数据到 setmeal表中 ： SetmealMapper
        setmealMapper.insert(setmealDto);

        //2、保存套餐菜品数据到 setmeal_dish表中： SetmealDishMapper
        //2.1 获取当前套餐的菜品列表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //2.2 循环遍历，获取每一个套餐菜品数据 setmealDish
        for (SetmealDish setmealDish : setmealDishes) {
            //2.3 获取套餐id，并set到setmealDish中
            Long id = setmealDto.getId();
            setmealDish.setSetmealId(id);
            //2.4 保存套餐菜品数据 setmealDish
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public void deleteSetmeal(List<Long> ids) {
        //1、判断当前要删除的套餐是否处于售卖状态，如果是，不允许删除
        // select count(*) from setmeal where id in (1,2,3) and status = 1
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(qw);
        if(count>0){//当前套餐存在售卖状态，不可删除 程序结束。
            throw new BusinessException("当前套餐存在售卖状态，不可删除");
        }

        //2、删除套餐： delete from setmeal where id in (1,2,3)
        setmealMapper.deleteBatchIds(ids);

        //3、删除套餐对应的菜品数据：delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> qw1 = new LambdaQueryWrapper<>();
        qw1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(qw1);
    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //1、根据id更新 setmeal 套餐表数据 update setmeal set xx=?,.. where id =?
        setmealMapper.updateById(setmealDto);

        //2、操作套餐对应的菜品数据： setmeal_dish
        //2.1 先根据套餐id删除套餐对应的菜品数据： delete from setmeal_dish where setmeal_id =?
        // 获取套餐id
        Long setmealId = setmealDto.getId();
        // 根据套餐id进行删除 ：delete from setmeal_dish where setmeal_id =?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishMapper.delete(qw);

        //2.2 重新添加新的套餐对应的菜品数据 insert setmeal_dish values (..)
        // setmealDto.getSetmealDishes()获取套餐的菜品集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 遍历套餐菜品集合数据
        for (SetmealDish setmealDish : setmealDishes) {
            // 有没有数据需要set ： 套餐id setSetmealId
            setmealDish.setSetmealId(setmealId);
            // 添加套餐菜品数据
            setmealDishMapper.insert(setmealDish);
        }
    }
}
